<template>
  <div class="ranking-list-get">
    <div style="margin: 2% 0; display: inline-block">
      <el-button type="primary" @click="onCreate">添加</el-button>
    </div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID"></el-table-column>
      <el-table-column prop="ranking" label="名次"></el-table-column>
      <el-table-column prop="headImg" label="头像">
        <template slot-scope="scope">
          <img :src="scope.row.headImg" style="width: 100px; height: 100px" />
        </template>
      </el-table-column>
      <el-table-column prop="userName" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="sum" label="收益"> </el-table-column>
      <el-table-column prop="numByTime" label="人数"> </el-table-column>
      <el-table-column label="操作" width="160px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            style="margin-bottom: 5px"
            size="mini"
            type="primary"
            @click="onEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            style="margin-bottom: 5px"
            size="mini"
            type="danger"
            @click="onDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center; margin-top: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        :current-page="page"
        layout="total,sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="`${dialogStatus === 'edit' ? '修改' : '新增'}`"
      :visible.sync="dialogFormVisible"
      center
    >
      <el-form :model="form" label-width="200px" :rules="rules" ref="ruleForm">
        <el-form-item label="名次：" prop="ranking">
          <el-input v-model="form.ranking" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="ID：" prop="id" v-if="dialogStatus === 'edit'">
          <el-input v-model="form.id" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="用户ID：" prop="userId">
          <el-input v-model="form.userId" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="userName">
          <el-input v-model="form.userName" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="头像：">
          <div
            style="
              width: 148px;
              height: 148px;
              border: 1px dashed #c0ccda;
              border-radius: 6px;
              text-align: center;
              line-height: 148px;
            "
          >
            <el-upload
              class="avatar-uploader"
              v-model="imgUrl"
              action="https://admin.koudaidj.com/game/alioss/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess1"
            >
              <img
                v-if="form.headImg"
                :src="form.headImg"
                class="avatar"
                style="border-radius: 6px; width: 148px; height: 148px"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="收益：">
          <el-input v-model="form.sum" style="width: 65%"></el-input>
        </el-form-item>
        <el-form-item label="人数：">
          <el-input v-model="form.numByTime" style="width: 65%"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm('ruleForm')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
      <!-- <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </div> -->
    </el-dialog>
  </div>
</template>

<script>
const formRest = {
  id: null,
  ranking: null,
  userId: null,
  userName: null,
  headImg: null,
  sum: null,
  numByTime: null,
};
export default {
  name: "RankingListGet",
  props: {
    type: {
      type: String,
      default: "1",
    },
  },
  data() {
    return {
      page: 0,
      size: 10,
      total: 10,
      tableData: [],
      dialogStatus: "edit",
      dialogFormVisible: false,
      form: Object.assign(formRest, {}),
      imgUrl: null,
      rules: {
        id: [{ required: true, message: "请填写id", trigger: "blur" }],
        userId: [{ required: true, message: "请填写用户id", trigger: "blur" }],
        ranking: [{ required: true, message: "请填写名次", trigger: "blur" }],
        userName: [{ required: true, message: "请填写名称", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      const { page, size, type } = this;
      const { data: res } = await this.$http({
        url: this.$http.adornUrl2("/statistics/findRankList"),
        method: "get",
        params: this.$http.adornParams({ page, size, type }),
      });
      const { data, status } = res;
      if (status === 0) {
        const { content, totalElements } = data;
        this.tableData = content;
        this.total = totalElements;
      }
    },
    onCreate() {
      this.dialogFormVisible = true;
      this.dialogStatus = "create";
      this.form = Object.assign(formRest, {});
    },
    confirm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const form = { ...this.form, type: this.type };
          const { data: res } = await this.$http({
            url: this.$http.adornUrl2("/statistics/saveRankList"),
            method: "post",
            params: this.$http.adornParams(form),
          });
          const { status } = res;
          if (status === 0) {
            this.getData();
            this.$refs.ruleForm.resetFields();
            this.dialogFormVisible = false;
          }
        } else {
          this.$message.error("请检查表单");
          return false;
        }
      });
    },
    onEdit(i, row) {
      this.dialogFormVisible = true;
      this.form = row;
      this.dialogStatus = "edit";
    },
    onDelete(i, { id }) {
      this.$confirm("确认删除？").then(async () => {
        await this.$http({
          url: this.$http.adornUrl2("/statistics/deleteRankList"),
          method: "get",
          params: this.$http.adornParams({ id }),
        });
        this.$message("删除成功");
        this.getData();
      });
    },
    handleSizeChange(e) {
      this.size = e;
      this.getData();
    },
    handleCurrentChange(e) {
      this.page = e;
      this.getData();
    },
    handleAvatarSuccess1(file) {
      this.form.headImg = file.data;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
